function moveArrayBy(array, sourceValue, targetValue, key) {
  let newArray = [...array]
  function findItem(arr) {
    for (let i = 0; i < arr.length; i++) {
      let item = arr[i];
      if (item[key] == sourceValue) {
        sourceParent = arr;
        sourceIndex = i;
      } else if (item[key] == targetValue) {
        targetParent = arr;
        targetIndex = i;
      }
      if (!sourceParent || !targetParent) {
        if (Array.isArray(arr[i].children)) {
          findItem(arr[i].children);
        }
      }
      if (sourceParent && targetParent) {
        break;
      }
    }
  }
  let sourceParent = null;
  let sourceIndex = null;
  let targetParent = null;
  let targetIndex = null;
  findItem(newArray);
  if (sourceParent && targetParent) {
    let temp = sourceParent[sourceIndex];
    sourceParent[sourceIndex] = targetParent[targetIndex];
    targetParent[targetIndex] = temp;
    // targetParent.splice();
  }
  console.log(newArray);
  return newArray
}
const newArray = moveArrayBy(
  [
    {
      id: 2222,
      component: 'Input',
    },
    {
      id: 22221,
      component: 'Input',
    },
    {
      id: 22222,
      remoteComponent: '',
      children: [
        {
          id: 3333,
          component: 'Input',
        },
        {
          id: 4444,
        },
      ],
    },
  ],
  2222,
  3333,
  'id'
);
console.log({ newArray },newArray[2].children);
